package com.zheng.russiaRec.view 
{
	import com.zheng.utils.DebugTrace;
	import com.zheng.russiaRec.model.P_App;
	import com.zheng.russiaRec.model.P_Checkerboard;
	import com.zheng.russiaRec.staticData.EventData;
	import com.zheng.russiaRec.view.components.UI_Checkerboard;
	import com.zheng.russiaRec.view.components.UI_Rec;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import com.zheng.russiaRec.view.components.common.AM_Container;

	/**
	 * @describe  	棋盘Mediator
	 * @author  	Mr.zheng
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class M_Checkerboard extends Mediator implements IMediator
	{
		public static const NAME:String = "M_NAME_CHECKERBOARD";
		
		private var delLineArr:Array;
		//帧计时
		private var lastTickNum:int;
		//积木自动下掉
		private var autoObj:Object;
		
		public function M_Checkerboard(viewComponent:Object = null) 
		{
			super(NAME, viewComponent);
			autoObj=new Object();
			autoObj.userOP=Keyboard.DOWN;
		}
		override public function listNotificationInterests():Array 
		{
			return [EventData.M_START_GAME,EventData.P_BRICK_CREATED, EventData.P_GROUP_UPDATE, EventData.C_DEL_LINE, EventData.M_DELLINE_AM_OVER
			,EventData.M_GAME_PAUSE,EventData.M_GAME_RESUME,EventData.C_GAMEOVER];
		}
		override public function handleNotification(notification:INotification):void 
		{
			switch (notification.getName()) 
			{
				case EventData.M_START_GAME:
					drawCB();
					addEventFrame();
					break;
				case EventData.P_BRICK_CREATED:
					updateLiveGroup();
					break;
				case EventData.P_GROUP_UPDATE:
					updateLiveAndFixedGroup();
					break;
				case EventData.C_DEL_LINE:
					var arr:Array=notification.getBody() as Array;
					clearLine(arr[0]);
					delEventFrame();
					break;
				case EventData.M_GAME_PAUSE:
					delEventFrame();
					break;
				case EventData.M_GAME_RESUME:
					addEventFrame();
					break;
				case EventData.M_DELLINE_AM_OVER:
					updateLiveAndFixedGroup();
					p_cb.clearFixedGroup();
					sendNotification(EventData.P_BRICK_CREATED);
					addEventFrame();
					break;
				case EventData.C_GAMEOVER:
					delEventFrame();
					break;
			}
		}
		
		/**
		 * 获取棋盘UI 
		 * @return 
		 * 
		 */
		public function get cbUI():UI_Checkerboard{
			return viewComponent.ui_cb as UI_Checkerboard;
		}
		//私有========================================================================================
		
		/**
		 * 获取棋盘proxy
		 */
		private function get cbP():P_Checkerboard{
			return facade.retrieveProxy(P_Checkerboard.NAME) as P_Checkerboard;
		}
		/**
		 * 根据名称获取rec
		 * @param	sx
		 * @param	sy
		 * @return
		 */
		private function getRec(sx:int,sy:int):UI_Rec {
			var rec:UI_Rec;
			rec = cbUI.getChildByName("rec" + sx + "_" + sy) as UI_Rec;
			return rec;
		}
		/**
		 * 画棋盘 
		 * 
		 */
		private function drawCB():void{
			DebugTrace.monsterDebug(this,"画棋盘");
			var rec:Rectangle=p_cb.getCbRec();
			cbUI.drawCB(rec.x,rec.y,rec.width,rec.height);
		}
		//设置帧事件
		private function onEnterFrame(e:Event):void{
			var tickNum:int=getTimer();
			if(tickNum-lastTickNum>p_app.autoSpeed){
				lastTickNum=tickNum;
				sendNotification(EventData.MC_BRICK_MOVE, autoObj);
			}
		}
		private function addEventFrame():void 
		{
			cbUI.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		//删除帧事件
		private function delEventFrame():void {
			cbUI.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		private function get p_app():P_App{
			return facade.retrieveProxy(P_App.NAME) as P_App;
		}
		private function updateLiveGroup():void{
			DebugTrace.monsterDebug(this, "创建积木");
			if(!isEmptyLiveGroup()){
				updateRecsStatus(p_cb.liveGroup,true);
			}
		}
		/**
		 * 更新积木视图
		 */
		private function updateLiveAndFixedGroup():void 
		{
			DebugTrace.monsterDebug(this, "更新积木视图");
			if (!isEmptyLiveToGroup()) {
				updateRecsStatus(p_cb.liveGroup,false);
				updateRecsStatus(p_cb.liveToGroup,true);
			}
			if (!isEmptyFixedToGroup()) {
				updateRecsStatus(p_cb.fixedGroup,false);
				updateRecsStatus(p_cb.fixedToGroup,true);
			}
		}
		private function isEmptyLiveGroup():Boolean{
			if(p_cb.liveGroup != null && p_cb.liveGroup.length!= 0){
				return false;
			}
			return true;
		}
		private function isEmptyLiveToGroup():Boolean{
			if(p_cb.liveToGroup != null && p_cb.liveToGroup.length!= 0){
				return false;
			}
			return true;
		}
		private function isEmptyFixedToGroup():Boolean{
			if(p_cb.fixedToGroup != null && p_cb.fixedToGroup.length != 0){
				return false;
			}
			return true;
		}
		/**
		 * 删除行 
		 * @param arr
		 * 
		 */
		private function clearLine(recVoArr:Array):void {
			delLineArr = recVoArr;
			
			gotoStatusForRecs(recVoArr, UI_Rec.REC_AM_DISAPPEAR);
			
			var firstRecVo:Array=recVoArr[0] as Array;
			var rec:UI_Rec=getRec(firstRecVo[0],firstRecVo[1]);
			var targetMc:MovieClip=AM_Container(rec.currDisplay).displayMc;
			targetMc.addFrameScript(targetMc.totalFrames - 1, 
				function():void {
					onAmDisappearEnd(targetMc);
					sendNotification(EventData.M_DELLINE_AM_OVER,targetMc);
				});
		}
		//消失动画结束
		private function onAmDisappearEnd(targetMc:MovieClip):void 
		{
			targetMc.addFrameScript(targetMc.totalFrames - 1, null);
			stopAmFromRecs(getAmsFromRecVos(delLineArr));
			delLineArr = null;
		}
		private function get p_cb():P_Checkerboard{
			var p_cb:P_Checkerboard = facade.retrieveProxy(P_Checkerboard.NAME) as P_Checkerboard;
			return p_cb;
		}
		//根据recVo取得rec中的动画元件
		private function getAmsFromRecVos(recVos:Array):Array{
			var recVo:Array;
			var rec:UI_Rec;
			var am:AM_Container;
			var ams:Array=[];
			for(var name:String in recVos){
				recVo=recVos[name] as Array;
				rec=getRec(recVo[0],recVo[1]);
				am=rec.currDisplay as AM_Container;
				ams.push(am);
			}
			return ams;
		}
		//停止动画
		private function stopAmFromRecs(amArr:Array):void{
			var num:int=amArr.length;
			var am:AM_Container;
			for(var i:int=0;i<num;i++){
				am=AM_Container(amArr[0]);
				am.stop();
			}
		}
		//将recs中的元素跳转到gotoStatus状态
		private function gotoStatusForRecs(recVos:Array,gotoStatus:String):void 
		{
			var num:int=recVos.length;
			var rec:UI_Rec;
			var recVo:Array;
			for(var i:int=0;i<num;i++){
				recVo=recVos[i] as Array;
				rec=getRec(recVo[0],recVo[1]);
				rec.gotoStatus(gotoStatus);
			}
		}
		//更新积木的状态
		private function updateRecsStatus(recArrs:Array,isShow:Boolean):void {
			var num:int = recArrs.length;
			if(!num)return;
			var rec:UI_Rec;
			var recArr:Array;
			for (var i:int = 0; i < num; i++)
			{
				recArr = recArrs[i] as Array;
				rec = getRec(recArr[0], recArr[1]);
				if(rec==null)break;
				if (isShow) {
					rec.gotoStatus(UI_Rec.REC_SHOW);
				}else {
					rec.gotoStatus(UI_Rec.REC_NORMAL);
				}
			}
		}
	}
	
}